package com.company.bookstore.validator.user_admin_module;

import com.company.bookstore.entities.user_admin_module.Roles;
import com.company.bookstore.exception.user_admin_module.CustomException;
import com.company.bookstore.qo.user_admin_module.AdminQo;
import com.company.bookstore.service.user_admin_module.AdminService;
import com.company.bookstore.service.user_admin_module.RoleService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * AdminQoRegisterValidator
 *https://www.cnblogs.com/qixuejia/p/4211428.html
 * https://blog.csdn.net/zx48822821/article/details/80743997
 * https://www.cnblogs.com/wt20/p/9599058.html
 *
 * @blame Android Team
 */
@Component
public class AdminQoRegisterValidator extends AbstractValidatorHandler implements InitializingBean {
    @Resource
    AdminService adminService;
    static final   Pattern usernamePattern  = Pattern.compile("\\w{8,20}");

    static  final   Pattern passwordPattern  = Pattern.compile("[\\w|\\pP]{8,20}");

    @Resource
    RoleService roleService;
    @Override
    public boolean doValidator(Object object) throws CustomException {
        AdminQo adminQo = (AdminQo)object;
        boolean hasPassword =  adminQo.getPassword()!=null;
        boolean hasUsername =  adminQo.getAdminName()!=null;
        Matcher correctUsername;
        boolean hasCorrectUsername;
        Matcher correctPassword;
        boolean hasCorrectPassword;
        if (hasUsername){
            correctUsername = usernamePattern.matcher(adminQo.getAdminName());
            hasCorrectUsername = correctUsername.matches();
            if (!hasCorrectUsername){
                throw  new CustomException("账号账户名格式不正确，要求8-20位字母，数字，不能含有标点符号",403);
            }
            if (adminService.loadUserByUsername(adminQo.getAdminName())!=null){
                throw   new CustomException("该账户已存在",403);
            }
        }else {
            throw  new CustomException("账号名不能为空",403);
        }
        if (hasPassword){
            correctPassword = passwordPattern.matcher(adminQo.getPassword());
            hasCorrectPassword = correctPassword.matches();
            if (!hasCorrectPassword){
                throw  new CustomException("账号密码格式不正确，要求8-20位字母，数字，可含有标点符号",403);
            }
        }else {
            throw  new CustomException("账号密码不能为空",403);
        }
        if (adminQo.getFkRoleId() !=null){
            Roles roles  =roleService.getById(adminQo.getFkRoleId());
            if (roles == null){
                throw  new CustomException("没有该角色信息",403);
            }
            if (roles.getRoleName().equals("ROLE_ADMIN")){
                throw  new CustomException("管理员角色无法添加",403);
            }
        }else {
            throw  new CustomException("管理员角色信息不能为空",403);
        }
        boolean containIllegalParams = adminQo.getDeleted()!=null || adminQo.getEnabled()!=null
                || adminQo.getId() != null ;
        if (containIllegalParams){
            throw  new CustomException("参数格式，数量不正确",403);
        }
        return true;
    }



    @Override
    public void afterPropertiesSet() throws Exception {
        ValidatorFactory.register(this.getClass().getName(),this);
    }
}
